package sg.edu.nus.dbd.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import sg.edu.nus.dbd.formbean.LoginFormBean;
import sg.edu.nus.dbd.util.MyLogger;

public class LoginAction extends DispatchAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		try {
			MyLogger.debug(this.getClass(), "execute::Start...");
			HttpSession session = req.getSession(false);

			LoginFormBean loginForm = (LoginFormBean) form;
			String username = loginForm.getUsername();
			String password = loginForm.getPassword();

			if (username != null && username.equals("admin")
					&& password != null && password.equals("password")) {
				session.setAttribute("LOGIN_ID", loginForm.getUsername());
				return mapping.findForward("success");
			} else {
				req.setAttribute("errorMsg", "Invalid User Name / Password!");
				loginForm.setUsername("");
				loginForm.setPassword("");
				return mapping.findForward("failure");
			}
		} catch (Exception e) {
			MyLogger.error(this.getClass(), e);
			return mapping.findForward("error");
		}
	}
}